<script type="text/javascript">
	$(window).ready(function(){
		$('#continue').on('click', function(){
			var company_id = $('#inputCompany').val();
			$('#step1').addClass('hide');
			$('#step2').removeClass('hide');
			$.ajax({
			  type: "POST",
			  url: "/templates/ajax",
			  data: { company: company_id},
			  dataType: "HTML",
			  success: function(data){
			  	$('#products').html(data);
			  }
			});
		});
	});
</script>
<form class="form" action="/templates/edit/<?php echo $templates->id?>" role="form" method="POST">
<div class="col-lg-12 clearfix">
	<h1>Create new order form</h1>
		<div id="step1">
			<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
				<h3>Step 1. Basic settings.</h3>
				<hr/>
				<div class="form-group">
					<label class="form-label" for="inputName">Order Form Name</label>
					<div class="controls">
						<input class="form-control" disabled="true" name="data[name]" type="text" id="inputName" value="<?php echo $templates->title; ?>" >
					</div>
				</div>
				<div class="form-group">
					<label class="form-label" for="inputCompany">Company</label>
Type					
<div class="controls">
						<input class="form-control" disabled="true" name="data[name]" type="text" id="inputName" value="<?php echo $companies_exist->title; ?>" >
					</div>
				</div>
			</div>
		</div>
		<div id="step2" class="hide">
			<div class="col-lg-12">
				<h3>Step 4. Products.</h3>
				<div id="products"></div>
				
			</div>
		</div>
</div>
<br/>
</div>
<script type="text/javascript">
	$(window).ready(function(){
		$('.header-checkbox').on('click', function(){
			if ($(this).is(':checked')){
				$(this).parent('th').parent('tr').parent('thead').parent('table').find('tbody :checkbox').prop('checked', true);
			}else{
				$(this).parent('th').parent('tr').parent('thead').parent('table').find('tbody :checkbox').prop('checked', false);
			}
		});
		$("#wrap").find('tr.discount-group').each(function(){
			var numberOfCheckedQty = $(this).find('input.min:checkbox:checked').length;
			var numberOfCheckedDis = $(this).find('input.discount:checkbox:checked').length;
			$(this).attr('data-minqty',numberOfCheckedQty);
			$(this).attr('data-discounts',numberOfCheckedDis);
		});
		$('.discount').on('click', function(){
			var numberOfCheckedQty = $(this).parent('th').parent('tr').find('input.min:checkbox:checked').length;
			console.log(numberOfCheckedQty);
			
			if ($(this).is(':checked')){
				var count = parseInt($(this).parent('th').parent('tr').data('discounts'))+1;
				$(this).parent('th').parent('tr').data('discounts', count);
			}else{
				var count = parseInt($(this).parent('th').parent('tr').data('discounts'))-1;
				$(this).parent('th').parent('tr').data('discounts', count);
			}
			console.log(count);
			if (count == numberOfCheckedQty){
				$(this).parent('th').parent('tr').find('.discount').each(function(){
					if ($(this).is(":checked")){ }else{
						$(this).prop('disabled', true);
					}
				});
			}else{
				$(this).parent('th').parent('tr').find('.discount').each(function(){
					$(this).prop('disabled', false);
				});
			}
		});
        
        $('.min').on('click', function(){
            var numberOfCheckedDis = $(this).parent('th').parent('tr').find('input.discount:checkbox:checked').length;
			console.log(numberOfCheckedDis);
			if ($(this).is(':checked')){
				var count = parseInt($(this).parent('th').parent('tr').data('minqty'))+1;
				$(this).parent('th').parent('tr').data('minqty', count);
			}else{
				var count = parseInt($(this).parent('th').parent('tr').data('minqty'))-1;
				$(this).parent('th').parent('tr').data('minqty', count);
			}
            console.log(count);
			if (count == numberOfCheckedDis){
				$(this).parent('th').parent('tr').find('.discount').each(function(){
					$(this).prop('disabled', true);
				}); 
			}else{
				$(this).parent('th').parent('tr').find('.discount').each(function(){
					//$(this).prop('checked', false);
					$(this).prop('disabled', false);
				});
			}
			
			if(count > 1){
				$(this).parent('th').parent('tr').find('.min').each(function(){
					if ($(this).is(":checked")){ }else{
						$(this).prop('disabled', true);
					}
				});
			}else{
				$(this).parent('th').parent('tr').find('.min').each(function(){
					$(this).prop('disabled', false);
				});
			}
		});
		
	});
	
</script>

<?php foreach ($search as $category){?>

<?php
	if(!empty($settings_exist)){
		$current_discount=array();
		foreach($settings_exist as $setting => $value_setting){
			if($setting == $category['category']->id)
				$current_discount =$value_setting;
		}
	}
	//var_dump($current_discount);
	if(!empty($minqty_exist)){
		$current_minqty=array();
		foreach($minqty_exist as $minqty => $value_minqty){
			if($minqty == $category['category']->id)
				$current_minqty =$value_minqty;
		}
	}
	//var_dump($current_minqty);
?>
	<div class="panel panel-default">
		<div class="panel-heading"><h4><?php echo $category['category']->title;?></h4></div>
		<div class="panel-body">
			<table class="table table-striped table-hover" style="width: 100%;">
			    <thead>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th colspan="3" style="text-align: center;" >Min Qty For Discount</th>
                        <th colspan="4" style="text-align: center;">Discount</th>
                    </tr>
			        <tr class="discount-group" data-category="<?php echo $category['category']->id;?>" data-discounts="0" data-minqty="0">
			            <th><input type="checkbox" class="header-checkbox"/></th>
			            <th>Photo</th>
			            <th>Title</th>
			            <th>Price</th>
						<?php
							if($minqty_default){
								foreach($minqty_default as $minqty=>$value){
									if ($value == 'true'){
									$checked="";
									if(isset($current_minqty) && !empty($current_minqty)){
										if(isset($current_minqty[0]))
											if($current_minqty[0]==$minqty)
												$checked="checked";
										else if(isset($current_minqty[1]))
											if($current_minqty[1]==$minqty)
												$checked="checked";
											
									}
						?>
						<th><input <?php echo $checked;?> type="checkbox" name="minqty[min-<?php echo $category['category']->id?>-<?php echo $minqty;?>]" class="min" /><?php echo $minqty;?></th>
						<?php
									}
								}
							}
						?>
                        
						
			            <?php if ($discounts_exist){ ?>
			            	<?php foreach ($discounts_exist as $discount => $value){?>
								
			            		<?php if ($value == 'true'){ 
									$checked="";
									if(isset($current_discount) && !empty($current_discount)){
										
										if(isset($current_discount[0]))
											if($current_discount[0]==$discount)
												$checked="checked";
										else if(isset($current_discount[1]))
											if($current_discount[1]==$discount)
												$checked="checked";
									} 
								?>
                                    <th><input <?php echo $checked;?> name="discounts[discount-<?php echo $category['category']->id?>-<?php echo $discount?>]" class="discount" type="checkbox"/> <?php echo (int)$discount/10;?>%</th>
                                      
			            		<?php }?>
			            	<?php }?>
			            <?php }?>
			        </tr>
			    </thead>
			    <tbody>
			        <?php 
						foreach ($category['products'] as $p){
							$checked="";
							foreach($dataProducts as $productTemp){
								if($productTemp->product_id == $p->id)
									$checked="checked";
							}
					?>
							
						
			            <tr>
			            	<td><input <?php echo $checked;?> type="checkbox" class="product-checkbox" name="products[id<?php echo $p->id;?>]" value="<?php echo $p->id?>"/></td>
			                <td><img width="32px" height="32px" src="/public/products/<?php echo $p->photo?>"/></td>
			            	<td><?php echo $p->title;?></td>
			            	<td><?php echo $p->price;?>$</td>
                            <td></td>
                            <td></td>
                            <td></td>
				            <?php if ($discounts_exist){ ?>
				            	<?php foreach ($discounts_exist as $discount => $value){?>
				            		<?php if ($value == 'true'){?>
				            			<td><?php echo round(($p->price - ((int)$discount/1000)*$p->price),2);?>$</td>
				            		<?php }?>
				            	<?php }?>
				            <?php }?>
			            </tr>
			        <?php }?>
			    </tbody>
			</table>
		</div>
	</div>
<?php }?>
    <div class="col-lg-12">
        <button type="submit" name="action" value="edit_template" class="btn btn-primary">Save change</button>
    </div>
</form>